<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <title>用 HTML 属性事件处理程序显示随机图片</title>
</head>
<body>
<img src="usa.gif" alt="usa" onclick="changeImg(this)">
<img src="mexico.gif" alt="mexico" onclick="changeImg(this)">
<script>
    var myImages = [
        'usa.gif',
        'canada.gif',
        'jamaica.gif',
        'mexico.gif'
    ];
    function changeImg(e) {
        var newImgNumber;
        do {
            newImgNumber = Math.round(Math.random() * 3);
        } while (e.src.indexOf(myImages[newImgNumber]) != -1);
        e.src = myImages[newImgNumber];
        e.alt = myImages[newImgNumber];
    }
</script>
</body>
</html>